package org.liuchang;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.liuchang.ext.TabbedPanel;
import org.liuchang.incomeAndExpenses.Detail;
import org.liuchang.util.PublicUtil;
import org.pushingpixels.flamingo.api.common.JCommandButton;
import org.pushingpixels.flamingo.api.common.JCommandButton.CommandButtonKind;
import org.pushingpixels.flamingo.api.ribbon.JRibbon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;
import org.pushingpixels.flamingo.api.ribbon.RibbonApplicationMenu;
import org.pushingpixels.flamingo.api.ribbon.RibbonApplicationMenuEntryPrimary;
import org.pushingpixels.flamingo.api.ribbon.RibbonElementPriority;
import org.pushingpixels.flamingo.api.ribbon.RibbonTask;
import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;
import org.pushingpixels.flamingo.api.ribbon.resize.IconRibbonBandResizePolicy;

public class MainFrame extends JRibbonFrame {
	private JCommandButton button1;
	private JCommandButton button2;
	private JCommandButton button3;
	private JCommandButton button4;
	private JRibbonBand band1;
	private RibbonTask taskFinance;
	private JTabbedPane tabbedPane;

	public MainFrame() {
		super("XYW");
		setSize(new Dimension(800, 600));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
//		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setApplicationIcon(PublicUtil.getResizableIconFromResource("res/evernote/2.png"));
		// 先获取Ribbon，然后在Ribbon上添加一个Task
		getRibbon().addTask(getTaskFinance());
//		getRibbon().addTask(getTask("caiwuss"));
		setApplicationMenu(getRibbon());
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
	}


	@SuppressWarnings({ "unchecked", "rawtypes" })
	private RibbonTask getTaskFinance() {
		band1 = new JRibbonBand("收入", null);
		band1.setResizePolicies((List) Arrays.asList(
				new CoreRibbonResizePolicies.None(band1
						.getControlPanel()),
				new IconRibbonBandResizePolicy(band1.getControlPanel())));
		button1 = new JCommandButton("支出",
				PublicUtil.getResizableIconFromResource("res/2.png"));
		button1.addActionListener(new AddTab("支出"));
		button2 = new JCommandButton("收入",
				PublicUtil.getResizableIconFromResource("res/3.png"));
		button2.addActionListener(new AddTab("收入"));
		button3 = new JCommandButton("结余",
				PublicUtil.getResizableIconFromResource("res/4.png"));
		button3.addActionListener(new AddTab("结余"));
		button4 = new JCommandButton("TEST", 
				PublicUtil.getResizableIconFromResource("res/5.png"));
		band1.addCommandButton(button1, RibbonElementPriority.TOP);
		band1.addCommandButton(button2, RibbonElementPriority.MEDIUM);
		band1.addCommandButton(button3, RibbonElementPriority.MEDIUM);
		band1.startGroup();
		band1.addCommandButton(button4, RibbonElementPriority.MEDIUM);
		button4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println(tabbedPane.getTabRunCount());
			}
		});
		taskFinance = new RibbonTask("财务", band1);
		return taskFinance;
	}

	static {
		try {
			javax.swing.UIManager
					.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel");
			JFrame.setDefaultLookAndFeelDecorated(true); // windows功能失效
			JDialog.setDefaultLookAndFeelDecorated(true); // Dialog功能失效
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	  // 设置左上角圆图标  
    private void setApplicationMenu(JRibbon ribbon) {  
        RibbonApplicationMenu ram = new RibbonApplicationMenu(); 
        RibbonApplicationMenuEntryPrimary ramep = new RibbonApplicationMenuEntryPrimary(PublicUtil.getResizableIconFromResource("res/2.png"), "text", new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("action");
			} 
		}, CommandButtonKind.POPUP_ONLY);
        ram.addMenuEntry(ramep);
        
        
        
        ribbon.setApplicationMenu(ram); // 左上角的圆图标  
    }  

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				MainFrame frame = new MainFrame();
//				frame.pack();
				frame.setVisible(true);
			}
		});
	}
	
	protected int selectedIndex(String title){
		int count = tabbedPane.getTabCount();
		for(int i=0;i<count;i++){
			if(tabbedPane.getTitleAt(i).equals(title)){
				return i;
			}
		}
		return -1;
	}
	
	private class AddTab implements ActionListener{

		private String title;
		
		public AddTab(String title){
			this.title = title;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			int index = selectedIndex(title);
			if(index == -1){
				int newIndex = tabbedPane.getTabCount();
				tabbedPane.add(title,new Detail());
				tabbedPane.setTabComponentAt(newIndex, new TabbedPanel(tabbedPane));
				tabbedPane.setSelectedIndex(newIndex);
			} else {
				tabbedPane.setSelectedIndex(index);
			}
		}
		
	}
}
